import 'package:atcflutter/base/network/atc_http_util.dart';
import 'package:atcflutter/user/login_page.dart';
import 'package:atcflutter/user/model/user_model.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'home/main_page.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  bool isLogin;

  @override
  void initState() {
    super.initState();
    //应用初始化操作
    ATCHttpUtil.setProxy("10.169.1.98:8888");
    ATCHttpUtil.setConnectTimeOut(5);
    //判断是否登录
    UserModel.isLogin().then((login) {
      setState(() {
        isLogin = login;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: BotToastInit(),
      navigatorObservers: [BotToastNavigatorObserver()],
      title: '车智赢+',
      theme: ThemeData(
        primarySwatch: Colors.deepOrange,
      ),
      home: _getHomePage(),
    );
  }

  Widget _getHomePage() {
    if (isLogin == null) {
      return _createSplashWidget();
    } else {
      return isLogin ? MainPage() : LoginPage();
    }
  }

  Widget _createSplashWidget() {
    return Center(
      child: Image.asset("image/logo_login.png"),
    );
  }
}
